package com.huawei.cloud.phone.management.domain.connection;

import com.huawei.cloud.phone.management.domain.phone.PhoneDomainService;
import com.huawei.cloud.phone.management.domain.phone.UserPhone;
import com.huawei.cloud.phone.management.domain.user.IUserEventCallback;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Component("connectionUserEventCallback")
public class UserEventCallback implements IUserEventCallback {

    private final ConnectionDomainService connectionDomainService;

    private final PhoneDomainService phoneDomainService;

    @Override
    public void forceLogout(String userId) {
        List<UserPhone> phones = phoneDomainService.getPhones(userId);
        if (phones.isEmpty()) {
            return;
        }
        connectionDomainService.disconnect(phones.stream().map(UserPhone::getPhoneId).collect(Collectors.toList()));
    }
}
